package com.xcw.test;

import java.io.IOException;
import java.util.List;
 
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
 
/**
 * 解析excel文件
 * @author guosk
 */
public class WriteExcelUtils {
 
    /**
     * 输出Excel文档
     * @param workbook 
     * @param resources 源数据
     * @param headerNames 表头
     * @param sheetName 表格名
     * @param columnNum 列数量
     * @param sheetNum 页码(sheet页码)
     * @throws IOException
     */
    public static void writeExcel(HSSFWorkbook workbook,
    					   List<String[]> resources, 
    					   String[] headerNames,
    					   String sheetName,
                                           Integer columnNum,
    					   Integer sheetNum) throws IOException {
        // 创建表格
    	HSSFSheet sheet = workbook.createSheet();
    	sheet.setDefaultRowHeightInPoints(13);//默认宽度
        workbook.setSheetName(sheetNum, sheetName);
        // 设置列宽，根据
        for(int i=0; i<=columnNum; i++){
            sheet.setColumnWidth(i, 6000);
        }
        /*
		 * 创建合并区域
		 * CellRangeAddress(int 首行, int 最后一行, int 首列, int 最后一列);
         */
        CellRangeAddress add = new CellRangeAddress(0, 0, 0, columnNum);
        // 将创建的合并区域设置到表格中.
        sheet.addMergedRegion(add);
        // 创建行
        Row header = sheet.createRow(0);
        // 创建单元格. 合并后的单元格,编号合并. 
        //设置样式
        CellStyle titleStyle = workbook.createCellStyle();
        Font titlefont = workbook.createFont();
        titlefont.setFontName("黑体");
        //titlefont.setColor(IndexedColors.VIOLET.index);
        titlefont.setFontHeightInPoints((short)20);
        titlefont.setBold(true);
        titleStyle.setFont(titlefont);
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        Cell c = header.createCell(0);
        c.setCellValue(sheetName);
        c.setCellStyle(titleStyle);
        c = header.createCell(columnNum);
        // 编写表头
        // 定义表头的样式
        CellStyle headerStyle = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontName("宋体");
        //font.setColor(IndexedColors.VIOLET.index);
        font.setFontHeightInPoints((short)16);
        headerStyle.setFont(font);
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        // 设置单元格样式
        Row headerRow = sheet.createRow(1);
        for (int i = 0; i < headerNames.length; i++) {
            Cell cell = headerRow.createCell(i);
            // 设置单元格样式
            cell.setCellStyle(headerStyle);
            cell.setCellValue(headerNames[i]);
        }
        // 设置表格数据的样式
        CellStyle bodyStyle = workbook.createCellStyle();
        Font bodyFont = workbook.createFont();
        bodyFont.setFontName("微软雅黑");
        //bodyFont.setColor(IndexedColors.BLUE.index);
        bodyFont.setFontHeightInPoints((short)12);
        bodyStyle.setFont(bodyFont);
 
        // 编辑表格体数据
        for (int i = 0; i < resources.size(); i++) {
            // 获取行数据
            String[] temp = resources.get(i);
            // 创建行
            Row bodyRow = sheet.createRow(i + 2);
            for (int cellNum = 0; cellNum < temp.length; cellNum++) {
                Cell bodyCell = bodyRow.createCell(cellNum);
                bodyCell.setCellStyle(bodyStyle);
                bodyCell.setCellValue(temp[cellNum]);
            }
        }
        sheet.getRow(0).setHeightInPoints(24);
        sheet.getRow(1).setHeightInPoints(20);
    }
}